classdef GIPackage < handle

  properties (Access=protected)
    symbols
    namespace = ''
  end

  methods

    function varargout = subsref(obj, S, noutrequested)
      if nargin < 3
        noutrequested = nargout;
        indirectReturn = false;
      else
        if nargout > 1, 'Too many outputs for indirect return', end
        indirectReturn = true;
      end
      if ~strcmp(S(1).type, '.'), error 'Unsupported subscript for GIPackage', end
      name = S(1).subs;
      if ~ obj.symbols.isKey(name)
        obj.symbols(name) = glib.g_irepository_matlab_find_symbol(obj.namespace, name);
      end  % try to find symbol
      symbol = obj.symbols(name);
      if length(S) == 1
        if nargout > 1, error 'Too many outputs for package symbol', end
        if indirectReturn, varargout{1} = { symbol };
        else varargout{1} = symbol; end
      else
        ret = subsref(symbol, S(2:length(S)), noutrequested);
        if indirectReturn, varargout{1} = ret;
        else varargout = ret; end
      end % only 1 subscript?
    end  % function subsref

    function s = char(pkg)
      s = sprintf('GIPakcage(%s)', pkg.namespace);
    end
    
    function disp(pkg)
      disp(char(pkg));
    end

    %% This is an internal constructor!
    %% To get a package use glib.gi.PKGNAME
    function obj = GIPackage(name)
      obj.symbols = containers.Map;
      obj.namespace = name;
    end
    
  end  % Public Methods

end  % Classdef
